<!DOCTYPE html>
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script type="text/javascript">
description("Test WebSocket subprotocol negotiation.");

window.jsTestIsAsync = true;

var ws = new WebSocket("ws://127.0.0.1:8880/protocol-test?protocol=superchat", ["chat", "superchat"]);
var closeEvent;
var receivedMessage;

shouldBeEqualToString("ws.protocol", "");

ws.onopen = function()
{
    debug("Connected");
    shouldBeEqualToString("ws.protocol", "superchat");
};

ws.onmessage = function(event)
{
    receivedMessage = event.data;
    debug("Received: " + receivedMessage);
    shouldBeEqualToString("receivedMessage", "superchat");
};

ws.onclose = function(event)
{
    debug("Closed");
    shouldBeEqualToString("ws.protocol", "superchat");
    closeEvent = event;
    shouldBeTrue("closeEvent.wasClean");
    setTimeout("checkAfterOnClose()", 0);
};

function checkAfterOnClose()
{
    debug("Exited onclose handler");
    shouldBeEqualToString("ws.protocol", "superchat");
    finishJSTest();
}

</script>
</body>
</html>
